const electron = require('electron');
const { BrowserWindow } = electron;

module.exports = (opts) => {
    const sub = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: false }, ...opts });

    addNewWindowEvent(sub);
    return sub;
};

function addNewWindowEvent(window) {
    window.webContents.on('new-window', (event, url, frameName, disposition, options, additionalFeatures) => {
        event.preventDefault();


        options = {
            ...options,
            parent: window,
        };

        if (window) {
            const [width, height] = window.getSize();
            options.width = width;
            options.height = height;
        }

        console.log(options);

        const subSubWindows = new BrowserWindow(options);
        subSubWindows.removeMenu();
        subSubWindows.loadURL(url);

        addNewWindowEvent(subSubWindows, window);

        event.newGuest = subSubWindows;
    });
}